package bean;

import json.person;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@WebServlet(name = "login_dgut")
public class login_dgut extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MakeLogin(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MakeLogin(request,response);

    }

    protected  void MakeLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        person dgut = (person) request.getSession().getAttribute("USER");
        User user_temp = (User) request.getSession().getAttribute("user");
        EntityManagerFactory entityManagerFactory = null;
        EntityManager em = null;
        User user = null;
        boolean flag = true;
        if (dgut != null) {
            try {
                entityManagerFactory = Persistence.createEntityManagerFactory("MyDSTest");

                em = entityManagerFactory.createEntityManager();

                Query query = em.createNativeQuery("select * from USER", User.class);
                List list = query.getResultList();
                if (list != null) {
                    Iterator<User> it = list.iterator();
                    while (it.hasNext()) {
                        User temp = (User) it.next();
                        if (temp.getId() != null && temp.getId().equals(dgut.getUsername())) {
                            user = new User(temp.getUsername(), temp.getEmail(), temp.getPasswd(), temp.getImg(), temp.getId());
                            flag = false;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                em.close();
            }

            if (flag && user_temp != null&&user_temp.getId()==null) {
                if (function.writeToUserid(dgut, user_temp) != null)
                {
                    request.getSession().removeAttribute("user");
                    request.getSession().removeAttribute("USER");
                    response.sendRedirect("bind_success.jsp");
                    return;
                }
                else response.sendRedirect("exist.jsp");
            }
            else if (flag) user = new User("游客", null, "123456", "image/default.jpg", dgut.getUsername());
            if(user!=null)
            {
                if(user.getUsername().equals("游客"))
                {
                    request.getSession().setAttribute("user",user);
                    response.sendRedirect("/dgut_makeName.jsp");
                }
                else
                {
                    request.getSession().setAttribute("user",user);
                    response.sendRedirect("/interface.jsp");
                }
            }
            else response.sendRedirect("/error.jsp");
        }
        else response.sendRedirect("/error.jsp");
    }
}
